<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    // 首页
    public function index() {

//        $length = 6;
//        $crypto_strong = true;
//        $bytes = openssl_random_pseudo_bytes($length, $crypto_strong);
//        $hex = bin2hex($bytes);
//        print_r($hex);exit();

        $sess_data = $this->session->userdata();
        if (isset($sess_data['role'])) {
            $data['is_login'] = TRUE;
        } else {
            $data['is_login'] = FALSE;
        }

        $this->db->select('site_desc');
        $site_ret = $this->db->get('ci_site_config')->result_array();
        $data['site_desc'] = $site_ret[0]['site_desc'];

        $data['post'] = array();
        $post_ret = $this->db->get_where('ci_post', array('post_status' => 1))
                ->last_row('array');
        $tags = $this->transform_tags($post_ret['tag_id']);
        $cat = $this->transform_cat($post_ret['cat_id']);
        $post_ret['tags'] = $tags;
        $post_ret['cat'] = $cat;
        $data['post'][] = $post_ret;

        // 最新文章
        $post_query = $this->db->select('post_id,post_title,post_create_time')
                ->where('post_status = 1')
                ->order_by('post_id', 'DESC')
                ->limit(5)
                ->get('ci_post');
        $data['post_list'] = $post_query->result_array();

        // 分类
        $cat_query = $this->db->select('*')
                ->where(array('cat_status' => 1))
                ->order_by('cat_id', 'DESC')
                ->get('ci_cat_config')
                ->result_array();

        foreach ($cat_query as $item) {
            $this->db->where(array('cat_id' => $item['cat_id']));
            $this->db->from('ci_post');
            $item['count'] = $this->db->count_all_results();
            $cat_list[] = $item;
        }
        $data['cat_list'] = $cat_list;

        // 标签
        $tag_query = $this->db->select('*')
                ->where('tag_status = 1')
                ->order_by('tag_id', 'DESC')
                ->get('ci_tags_config');

        $data['tag_list'] = $tag_query->result_array();

        $this->load->view('/blog/_head', $data);
        $this->load->view('/blog/index');
        $this->load->view('/blog/_foot');
    }

    // 汇总
    public function gather() {
        $this->load->view('/blog/todo');
    }

    // 博文展示
    public function article($post_id) {

        // 点击量+1
        $this->db->set('post_views', 'post_views+1', FALSE);
        $this->db->where('post_id', $post_id);
        $this->db->update('ci_post');

        $sess_data = $this->session->userdata();
        if (isset($sess_data['role'])) {
            $data['is_login'] = TRUE;
        } else {
            $data['is_login'] = FALSE;
        }

        $this->db->select('site_desc');
        $ret = $this->db->get('ci_site_config')->result_array();
        $data['site_desc'] = $ret[0]['site_desc'];

        $data['post'] = array();
        $ret = $this->db->get_where('ci_post', array('post_id' => $post_id))
                ->result_array();
        foreach ($ret as $item) {
            $tags = $this->transform_tags($item['tag_id']);
            $cat = $this->transform_cat($item['cat_id']);
            $item['tags'] = $tags;
            $item['cat'] = $cat;
            $data['page_title'] = $item['post_title'];
            $data['post'][] = $item;
        }

        // 最新文章
        $post_query = $this->db->select('post_id,post_title,post_create_time')
                ->where('post_status = 1')
                ->order_by('post_id', 'DESC')
                ->limit(5)
                ->get('ci_post');
        $data['post_list'] = $post_query->result_array();

        // 分类
        $cat_query = $this->db->select('*')
                ->where(array('cat_status' => 1))
                ->order_by('cat_id', 'DESC')
                ->get('ci_cat_config')
                ->result_array();

        foreach ($cat_query as $item) {
            $this->db->where(array('cat_id' => $item['cat_id']));
            $this->db->from('ci_post');
            $item['count'] = $this->db->count_all_results();
            $cat_list[] = $item;
        }
        $data['cat_list'] = $cat_list;

        // 标签
        $tag_query = $this->db->select('*')
                ->where('tag_status = 1')
                ->order_by('tag_id', 'DESC')
                ->get('ci_tags_config');

        $data['tag_list'] = $tag_query->result_array();

        //        var_dump($data['post']);
        $this->load->view('/blog/_head', $data);
        $this->load->view('/blog/article');
        $this->load->view('/blog/_foot');
    }

    // 留言墙
    public function pinboard() {
        $sess_data = $this->session->userdata();
        if (isset($sess_data['role'])) {
            $data['is_login'] = TRUE;
        } else {
            $data['is_login'] = FALSE;
        }

        $this->db->select('site_desc');
        $ret = $this->db->get('ci_site_config')->result_array();
        $data['site_desc'] = $ret[0]['site_desc'];
        $data['page_title'] = "Lei's Blog | PingBoard";
        $data['pings'] = $this->db->order_by('ping_id', 'DESC')->get('ci_ping')->result_array();

        $this->load->view('/blog/_head', $data);
        $this->load->view('/blog/pinboard');
    }

    // 图床
    public function gallery() {
        $sess_data = $this->session->userdata();
        if (isset($sess_data['role'])) {
            $data['is_login'] = TRUE;
        } else {
            $data['is_login'] = FALSE;
        }

        $this->db->select('site_desc');
        $ret = $this->db->get('ci_site_config')->result_array();
        $data['site_desc'] = $ret[0]['site_desc'];

        $data['gallery'] = array();
        $ret = $this->db->get_where('ci_gallery', array('photo_status' => 1))
                ->result_array();
        $data['gallery'] = $ret;
        $data['page_title'] = "Lei's Blog | Gallery";

        $this->load->view('/blog/_head', $data);
        $this->load->view('/blog/gallery');
    }

    // 关于我
    public function about() {
        $sess_data = $this->session->userdata();
        if (isset($sess_data['role'])) {
            $data['is_login'] = TRUE;
        } else {
            $data['is_login'] = FALSE;
        }

        $this->db->select('site_desc');
        $ret = $this->db->get('ci_site_config')->result_array();
        $data['site_desc'] = $ret[0]['site_desc'];
        $data['page_title'] = "Lei's Blog | About";

        $this->load->view('/blog/_head', $data);
        $this->load->view('/blog/about');
        $this->load->view('/blog/_foot');
    }

    // 留言板
    public function ajax_set_ping_config() {
        $ping_title = $this->input->post('ping_title');
        $ping_content = $this->input->post('ping_content');
        $data = array(
            'ping_title' => $ping_title,
            'ping_content' => $ping_content,
            'ping_create_time' => time(),
        );
        $ret = $this->db->insert('ci_ping', $data);
        if ($ret) {
            $data['ping_time'] = date('Y-m-d H:m:s', $data['ping_create_time']);
            $this->output->set_header('Content-Type: application/json; charset=utf-8');
            $this->echoJson($data);
        } else {
            echo FALSE;
        }
    }

    function echoJson($arr) {
        header('Content-Type: application/json; charset=utf-8');
        if (strpos(PHP_VERSION, '5.3') > -1) {
            // php 5.3-
            echo json_encode($arr);
        } else {
            // php 5.4+
            echo json_encode($arr, JSON_UNESCAPED_UNICODE);
        }
    }

    function transform_tags($data) {
        $tags = array();

        if ($data) {
            $tmp = explode(',', $data);
            foreach ($tmp as $tmp_item) {
                $this->db->select('tag_name');
                $query = $this->db->get_where('ci_tags_config', array('tag_id' => $tmp_item))->result_array();
                $tags[] = $query[0];
            }
        } else {
            $tags = FALSE;
        }

        return $tags;
    }

    function transform_cat($data) {
        if ($data) {
            $this->db->select('cat_name');
            $query = $this->db->get_where('ci_cat_config', array('cat_id' => $data))->result_array();
            $cat = $query[0];
        } else {
            $cat = FALSE;
        }

        return $cat;
    }

}
